﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows.Media.Imaging;
using iMapa.Backend;
using iMapa.Utils;
using System.Windows;
using System.ComponentModel;
using iMapa.UI.ModelView.Common;
using iMapa.Helper;

namespace iMapa.UI.ModelView.Main
{
    public class SimulationGroupsViewModel :ViewModelBase
    {
        private ObservableCollection<GroupViewModel> groups = new ObservableCollection<GroupViewModel>();
        private MainViewModel root;

    
        public SimulationGroupsViewModel(MainViewModel root)
        {
           this.root = root;
           reloadViewModels();
        }

        private void reloadViewModels()
        {
            groups.Clear();
            List<InteractMapGroup> interactGroups = ListOfInteractMapGroups.getInstance().GetAllInteractMapGroups();
            foreach (InteractMapGroup interactGroup in interactGroups)
            {
                List<InteractMap> maps = interactGroup.GetAllInteractMaps();
                GroupViewModel mapsGroup = new GroupViewModel(root);
                mapsGroup.Name = interactGroup.Name;
                mapsGroup.Parent = this;
                mapsGroup.Igroup = interactGroup;
                foreach (InteractMap imap in maps)
                {
                    MapViewModel map = new MapViewModel(root, imap);
                    map.Image = ImgUtils.ToBitmapSource(imap.ModelImage);
                    mapsGroup.Maps.Add(map);
                }
                groups.Add(mapsGroup);
            }
        }

        public void AddNewMap(InteractMap map)
        {
            if (map != null)
            {
                List<InteractMapGroup> interactGroups = ListOfInteractMapGroups.getInstance().GetAllInteractMapGroups();
                string groupname = map.ParentGroup;
                InteractMapGroup targetGroup = null;
                foreach (InteractMapGroup img in interactGroups)
                {
                    if (groupname != null && groupname.Equals(img.Name))
                    {
                        targetGroup = img;
                        break;
                    }
                }
                if (targetGroup != null)
                {
                    targetGroup.Maps.Add(map);
                }
                InteractMapLoader.Save(map);
                reloadViewModels();
            }
        }

        public void AddNewGroup()
        {
            InteractMapGroup img = new InteractMapGroup();
            img.Name = "Enter name of group...";
            GroupViewModel g= new GroupViewModel(root);
            g.Name = img.Name;
            g.Parent = this;
            g.IsNew= true;
            g.Igroup=img;
            groups.Add(g);
            ListOfInteractMapGroups.getInstance().AddInteractMapGroup(img);
        }

        public void removeGroup(GroupViewModel g)
        {
            groups.Remove(g);
        }

        public ObservableCollection<GroupViewModel> Groups
        {
            get { return groups; }
            set { groups = value; }
        }
        
        public MainViewModel Root
        {
            get { return root; }
            set { root = value; }
        }
    }



}
